set more off, perm

global date : di %tdCCYYNNDD date(c(current_date), "DMY") 
global google: env google
global work $google\SPIRE\Payments\Public
global out $google\SPIRE\Payments\Public\Stata-out
global fig $google\SPIRE\Payments\Public\Figures
cd "$work"


use "$work\Stata\country-public.dta" , clear

// specify sample
global cntry inlist(ccode, "AT", "AU", "BE", "CA", "DE", "DK", "EE", "ES") | ///
  inlist(ccode, "FI", "GB", "LT", "LV", "NO", "PL", "PT", "SE") | ///
  inlist(ccode, "SI", "US", "TW", "KR")

global sample ($cntry) & !mi(wage_cashier_ipolate) ///
  & !mi(cash_ipolate) & !mi(gdp_retail_propn)  
summ wage_cashier_ipolate if $sample
global wage_avg = r(mean)
summ cash_ipolate if $sample 
global cash_sd = r(sd)

bysort ccode (year) : gen ctrylength = year - 2008
replace ctrylength = . if ctrylength < 0
summ ctrylength if $sample

order ccode year cash_ipolate wage_cashier*

***** Table 1. Summary statistics ****  
su $varlist if $sample , format 
estpost summ cash_ipolate wage_cashier_ipolate gdp_pc popn_mm gdp_retail_propn ///
   tax wage_ctrlstr_ipolate if $sample
mat SUMM = e(count)',e(mean)',e(sd)',e(min)',e(max)'
mat colnames SUMM = N Mean Std_dev Min Max
estout mat(SUMM,fmt(%7.2f)) using "$work\Stata-out\summstat.xls" , replace ///
	label type mlabels(,none)   
tab ccode if $sample
unique ccode if $sample

***** Table 2. Estimates: Fixed effects ****  
global depvar wage_cashier_ipolate
global control popn_mm gdp_pc 
global table 2fe
global outopt landscape plain coljust(lc) var se starloc(1) starlevels(10 5 1) ///
  summstat(N \ N_clust \ r2_w \ r2_b \ est \ bootp ) ///
  summtitle("Observations" \ "Countries" \ "R2-within" \ "R2-between" \ ///
  "Economic effect" \ "Bootstrap p-val") ///
  nocons
global outopt2 plain coljust(lc) landscape

xtset iso3n year
outreg, clear(t$table)

xtreg $depvar cash_ipolate $control if $sample , fe 
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectry addrows("Country fixed effects", "Yes" \ "Year fixed effects", "No")
outreg, merge(t$table) $outopt ///
  ctitle("VARIABLES", "(a)" \ "", "Country fixed effects") ///
  keep(cash_ipolate $control) $fectry
  

xtreg $depvar cash_ipolate $control i.year ///
  if $sample , fe
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
reghdfe $depvar cash_ipolate $control i.year ///
  if $sample , abs(ccode) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
	reghdfe $depvar cash_ipolate $control i.year ///
	if $sample , abs(ccode) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectryyr addrows("Country fixed effects", "Yes" \ "Year fixed effects", "Yes")
outreg, merge(t$table) $outopt ///
  ctitle("VARIABLES", "(b)" \ "", "Country & year f.e.") ///
  keep(cash_ipolate $control) $fectryyr  

xtreg $depvar cash_ipolate $control gdp_retail_propn i.year ///
  if $sample , fe
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
	reghdfe $depvar cash_ipolate $control gdp_retail_propn i.year ///
	if $sample , abs(ccode) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
reghdfe $depvar cash_ipolate $control gdp_retail_propn i.year ///
  if $sample , abs(ccode) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectryyr addrows("Country fixed effects", "Yes" \ "Year fixed effects", "Yes")
outreg, merge(t$table) $outopt ///
  ctitle("VARIABLES", "(c)" \ "", "Control retail GDP") ///
  keep(cash_ipolate $control gdp_retail_propn) $fectryyr  

xtreg $depvar cash_ipolate $control tax i.year ///
  if $sample , fe
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
	reghdfe $depvar cash_ipolate $control tax i.year ///
	if $sample , abs(ccode) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
reghdfe $depvar cash_ipolate $control tax i.year ///
  if $sample , abs(ccode) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectryyr addrows("Country fixed effects", "Yes" \ "Year fixed effects", "Yes")
outreg, merge(t$table) $outopt ///
  ctitle("VARIABLES", "(d)" \ "", "Control corporate income tax") ///
  keep(cash_ipolate $control tax) $fectryyr  
    

xtreg wage_ctrlstr_ipolate cash_ipolate $control i.year ///
  if $sample , fe
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
	reghdfe wage_ctrlstr_ipolate cash_ipolate $control i.year ///
	if $sample , abs(ccode) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
reghdfe wage_ctrlstr_ipolate cash_ipolate $control i.year ///
  if $sample , abs(ccode) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectryyr addrows("Country fixed effects", "Yes" \ "Year fixed effects", "Yes")   
outreg, merge(t$table) $outopt ///
  ctitle("VARIABLES", "(e)" \ "", "Falsification") ///
  keep(cash_ipolate $control) $fectryyr

outreg using "$out\cwage[$date].doc", replay(t$table) $outopt2 replace ///
  title("Table 2. Cashier wages and consumer payment in cash") ///
  note("Notes: Estimated by REGHDFE; Dependent variable: Cash usage" ///
    "Robust standard errors clustered by country (***p < 0.01 **p < 0.05 *p < 0.1)" ) 
  
  
***** Table 2RE. Estimates: Random effects ****  
global depvar wage_cashier_ipolate
global control popn_mm gdp_pc 
global table re
global outopt landscape plain coljust(lc) var se starloc(1) starlevels(10 5 1) ///
  summstat(N \ r2_w \ r2_b) ///
  summtitle("Observations" \ "R2-within" \ "R2-between") nocons

outreg, clear($table)

xtreg $depvar cash_ipolate $control if $sample , re cluster(iso3n)
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate)
test (_b[cash_ipolate] * $cash_sd) /$wage_avg = 0 
global p_value: display %5.3f r(p)  
global fectry addrows("Year fixed effects", "No" \ ///
   "Marginal effect", "$est" \ "p-value", "$p_value")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(a)" \ "", "Country random effects") ///
  keep(cash_ipolate $control) $fectry

bootstrap _b[cash_ipolate] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///
xtreg $depvar cash_ipolate $control if $sample , re cluster(ccode)
  

xtreg $depvar cash_ipolate $control i.year if $sample , re cluster(ccode)
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate)
test (_b[cash_ipolate] * $cash_sd) /$wage_avg = 0 
global p_value: display %5.3f r(p)  
global fectry addrows("Year fixed effects", "Yes" \ ///
   "Marginal effect", "$est" \ "p-value", "$p_value")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(b)" \ "", "Country r.e. & year f.e.") ///
  keep(cash_ipolate $control) $fectry 

bootstrap _b[cash_ipolate] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///
xtreg $depvar cash_ipolate $control i.year if $sample , re cluster(ccode)
 
 
xtreg $depvar cash_ipolate $control gdp_retail_propn i.year if $sample , ///
  re cluster(ccode) 
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate)
test (_b[cash_ipolate] * $cash_sd) /$wage_avg = 0 
global p_value: display %5.3f r(p)  
global fectry addrows("Year fixed effects", "Yes" \ ///
   "Marginal effect", "$est" \ "p-value", "$p_value")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(c)" \ "", "Control retail GDP") ///
  keep(cash_ipolate $control gdp_retail_propn) $fectry
  
bootstrap _b[cash_ipolate] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///  
xtreg $depvar cash_ipolate $control gdp_retail_propn i.year if $sample , ///
  re cluster(ccode) 

outreg using "$out\cwage_re.doc", replay($table) $outopt2 replace ///
  title("Table 2RE. Cashier wages and consumer payment in cash") ///
  note("Notes: Estimated by XTREG; Dependent variable: Cashier wage (USD);" ///
  "Robust standard errors clustered by country in parentheses" ///
  "(***p < 0.01 **p < 0.05 *p < 0.1)" ) 
  
  
***** Table 2FD. Estimates: First differences ****  
xtset iso3n year   
foreach var in gdp_pc popn_mm cash_ipolate gdp_retail_propn tax ///
   wage_ctrlstr_ipolate wage_cashier_ipolate{
   gen `var'_fd = d.`var'
   }
label var gdp_pc_fd "FD GDP per capita"
label var popn_mm_fd "FD population"
label var cash_ipolate_fd "FD cash usage"
label var gdp_retail_propn_fd "FD retail GDP"
label var tax_fd "FD corporate tax"
label var wage_ctrlstr_ipolate_fd "FD similar occupation wage"

global depvar wage_cashier_ipolate_fd
global control popn_mm_fd gdp_pc_fd 
global table fd
global outopt landscape plain coljust(lc) var se starloc(1) starlevels(10 5 1) ///
  summstat(N \ r2) summtitle("Observations" \ "R2") nocons
global outopt2 plain coljust(lc) landscape

outreg, clear($table)

reg $depvar cash_ipolate_fd $control ///
  if $sample , cluster(ccode) nocon
lincom (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate)
test (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg = 0 
global p_value: display %5.3f r(p)  
global fectry addrows("Year fixed effects", "No" \ ///
   "Economic effect", "$est" \ "p-value", "$p_value")
outreg,  merge($table) $outopt ///
  ctitle("VARIABLES", "(a)" \ "", "First differences") ///
  keep(cash_ipolate_fd $control) $fectry

bootstrap _b[cash_ipolate_fd] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///  
reg $depvar cash_ipolate_fd $control ///
  if $sample , cluster(ccode) nocon
  
reg $depvar cash_ipolate_fd $control i.year ///
  if $sample , cluster(ccode) nocon
lincom (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate) 
test (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg = 0
global p_value: display %5.3f r(p)   
global fectryyr addrows("Year fixed effects", "Yes" \ ///
   "Economic effect", "$est" \ "p-value", "$p_value")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(b)" \ "", "Year f.e.") ///
  keep(cash_ipolate_fd $control) $fectryyr
  
bootstrap _b[cash_ipolate_fd] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///
reg $depvar cash_ipolate_fd $control i.year ///
  if $sample , cluster(ccode) nocon  

reg $depvar cash_ipolate_fd $control gdp_retail_propn_fd i.year ///
  if $sample , cluster(ccode) 
lincom (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg
global est: display %5.3f r(estimate) 
test (_b[cash_ipolate_fd] * $cash_sd) /$wage_avg = 0
global p_value: display %5.3f r(p) 
global fectryyr addrows("Year fixed effects", "Yes" \ ///
   "Economic effect", "$est" \ "p-value", "$p_value")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(c)" \ "", "Control retail GDP") ///
  keep(cash_ipolate_fd $control gdp_retail_propn_fd) $fectryyr  
  
bootstrap _b[cash_ipolate_fd] , seed(12345) reps(999) ///
  idcluster(iso3n) cluster(ccode): ///
reg $depvar cash_ipolate_fd $control gdp_retail_propn_fd i.year ///
  if $sample , cluster(ccode)
  
reg $depvar cash_ipolate_fd $control gdp_retail_propn_fd i.year ///
  if $sample , cluster(iso3n)  
boottest cash_ipolate_fd   

outreg using "$out\cwage_fd.doc", replay($table) $outopt2 replace ///
  title("Table 2RE. Cashier wages and consumer payment in cash") ///
  note("Notes: Estimated by REG; Dependent variable: First difference of cash usage" ///
    "Robust standard errors clustered by country (***p < 0.01 **p < 0.05 *p < 0.1)" ) 
  

****************************************  
*** Table 2FE. Response to Referee 1 ***
****************************************
global depvar wage_cashier_ipolate
global control popn_mm gdp_pc gdp_retail_propn
global sample ($cntry) & !mi(wage_cashier_ipolate) ///
  & !mi(cash_ipolate) & !mi(gdp_retail_propn)  
global outopt landscape plain coljust(lc) var se starloc(1) starlevels(10 5 1) ///
  summstat(N \ N_clust \ r2_w \ r2_b \ est \ bootp ) ///
  summtitle("Observations" \ "Countries" \ "R2-within" \ "R2-between" \ ///
  "Economic effect" \ "Bootstrap p-val") ///
  nocons
global table fespec  
  
outreg, clear($table)

xtreg $depvar cash_ipolate $control if $sample , fe 
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345)  : ///
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode year) 
lincom cash_ipolate
local bootp = r(p)
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode year) vce(robust)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectry addrows("Country fixed effects", "Yes" \ "Year fixed effects", "No")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(a)" \ "", "FE: Robust s.e.") ///
  keep(cash_ipolate $control) $fectry 

xtreg $depvar cash_ipolate $control if $sample , fe 
local r2_w = e(r2_w)
local r2_b = e(r2_b)
local r2_o = e(r2_o)
bootstrap, reps(999) seed(12345) idcluster(iso3n) cluster(ccode): ///
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode year) cluster(ccode)
lincom cash_ipolate
local bootp = r(p)
reghdfe $depvar cash_ipolate $control if $sample , abs(ccode year) cluster(ccode)
estadd scalar r2_w `r2_w'
estadd scalar r2_b `r2_b'
estadd scalar r2_o `r2_o'
lincom (_b[cash_ipolate] * $cash_sd) /$wage_avg
estadd scalar est r(estimate)
estadd scalar bootp `bootp'
global fectry addrows("Country fixed effects", "Yes" \ "Year fixed effects", "No")
outreg, merge($table) $outopt ///
  ctitle("VARIABLES", "(b)" \ "", "FE: Clustered s.e.") ///
  keep(cash_ipolate $control) $fectry
   
outreg using "$out\cwage_fe_spec.doc", replay($table) $outopt2 replace ///
  title("Table 2FESPEC. Cashier wages and consumer payment in cash")   

********************
***** Figure 1 *****
********************
 
gen wage_gdp_cashier = wage_cashier_usd / gdp_pc 
label variable wage_gdp_cashier "Cashier wage / GDP per capita"

global x pct_cash_trans
global y wage_gdp_cashier 
global z wage_gdp_ctrlstr   
global cntry inlist(ccode, "AT", "AU", "BE", "CA", "DE", "DK", "EE", "ES") | ///
  inlist(ccode, "FI", "GB", "LT", "LV", "NO", "PL", "PT", "SE") | ///
  inlist(ccode, "SI", "US", "TW", "KR")
global sample ($cntry) & !mi(wage_cashier_ipolate) ///
  & !mi(cash_ipolate) & !mi(gdp_retail_propn)  

twoway (scatter $y $x if $sample , ///
   mlabel(ccode) msymbol(O) mcolor(navy)) ///
   (lfit $y $x  if $sample , ///
	  lpattern(solid) lwidth(medthick) lcolor(navy)) , ///
	  xtitle("Percentage cash transactions") ///
	  ytitle("Annual wage / GDP per capita") ///
	  title(" ")   // Occupational wages and cash usage
graph export "$work\Stata-out\wage_cash.png", replace

tab ccode if $sample & !mi($x) & !mi($y)

